<?php
require_once 'database21.php';

class Teacher extends Database {
    public function createTeacher($name, $gender, $title, $contact_info, $department_id, $age, $basic_salary, $bonus) {
        try {
            $stmt = $this->conn->prepare("INSERT INTO teachers (teacher_name, gender, title, contact_info, department_id, age, basic_salary, bonus) VALUES (:name, :gender, :title, :contact_info, :department_id, :age, :basic_salary, :bonus)");
            $stmt->execute([
                ':name' => $name,
                ':gender' => $gender,
                ':title' => $title,
                ':contact_info' => $contact_info,
                ':department_id' => $department_id,
                ':age' => $age,
                ':basic_salary' => $basic_salary,
                ':bonus' => $bonus
            ]);
            return $this->conn->lastInsertId();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function getTeachers() {
        try {
            $stmt = $this->conn->prepare("
                SELECT t.*, d.department_name 
                FROM teachers t 
                LEFT JOIN departments d ON t.department_id = d.department_id
            ");
            $stmt->execute();
            return $stmt->fetchAll();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function getTeacher($id) {
        try {
            $stmt = $this->conn->prepare("
                SELECT t.*, d.department_name 
                FROM teachers t 
                LEFT JOIN departments d ON t.department_id = d.department_id 
                WHERE t.teacher_id = :id
            ");
            $stmt->execute([':id' => $id]);
            return $stmt->fetch();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function updateTeacher($id, $name, $gender, $title, $contact_info, $department_id, $age, $basic_salary, $bonus) {
        try {
            $stmt = $this->conn->prepare("UPDATE teachers SET teacher_name = :name, gender = :gender, title = :title, contact_info = :contact_info, department_id = :department_id, age = :age, basic_salary = :basic_salary, bonus = :bonus WHERE teacher_id = :id");
            $stmt->execute([
                ':id' => $id,
                ':name' => $name,
                ':gender' => $gender,
                ':title' => $title,
                ':contact_info' => $contact_info,
                ':department_id' => $department_id,
                ':age' => $age,
                ':basic_salary' => $basic_salary,
                ':bonus' => $bonus
            ]);
            return $stmt->rowCount();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function deleteTeacher($id) {
        try {
            // 检查是否有课程关联到该教师
            $stmt = $this->conn->prepare("SELECT * FROM courses WHERE teacher_id = :id");
            $stmt->execute([':id' => $id]);

            if ($stmt->rowCount() > 0) {
                throw new Exception("无法删除, 该教师有关联的课程存在");
            }

            // 检查是否有授课记录关联到该教师
            $stmt = $this->conn->prepare("SELECT * FROM teaching WHERE teacher_id = :id");
            $stmt->execute([':id' => $id]);

            if ($stmt->rowCount() > 0) {
                throw new Exception("无法删除, 该教师有授课记录存在");
            }

            $stmt = $this->conn->prepare("DELETE FROM teachers WHERE teacher_id = :id");
            $stmt->execute([':id' => $id]);
            return $stmt->rowCount();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        } catch (Exception $e) {
            die("错误: " . $e->getMessage());
        }
    }

    // 获取所有系部信息，用于教师管理中的系部选择
    public function getDepartments() {
        try {
            $stmt = $this->conn->prepare("SELECT * FROM departments");
            $stmt->execute();
            return $stmt->fetchAll();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }
}
?>